package org.yty.travelagencyredis.service.impl;

import cn.hutool.core.util.IdUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.yty.travelagencyredis.Dao.TourGroupDao;
import org.yty.travelagencyredis.model.Entity.TourGroup;
import org.yty.travelagencyredis.service.SignUpService;
import org.yty.travelagencyredis.service.TourGroupService;
import org.yty.travelagencyredis.service.TourGroupTravellerService;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class TourGroupServiceImpl implements TourGroupService {
    private final TourGroupDao tourGroupDao;
    @Lazy
    @Autowired
    private TourGroupTravellerService tourGroupTravellerService;
    @Lazy
    @Autowired
    private SignUpService signUpService;

    @Override
    public TourGroup getById(Long id) {
        return this.tourGroupDao.selectById(id);
    }

    @Override
    public boolean save(TourGroup tourGroup) {
        if (tourGroup.getId() == null){
            tourGroup.setId(IdUtil.getSnowflakeNextId());
        }
        tourGroup.setCreateTime(LocalDateTime.now());
        tourGroup.setUpdateTime(LocalDateTime.now());
        return this.tourGroupDao.insert(tourGroup);
    }

    @Override
    public boolean update(TourGroup tourGroup) {
        tourGroup.setUpdateTime(LocalDateTime.now());
        return this.tourGroupDao.updateById(tourGroup);
    }

    @Override
    public void removeById(Long id) {
        this.tourGroupTravellerService.deleteByTourGroupId(id);
        this.signUpService.deleteByTourGroupId(id);
        this.tourGroupDao.deleteById(id);
    }

    @Override
    public List<TourGroup> getListByIdList(List<Long> tourGroupIdList) {
        return this.tourGroupDao.getListByIdList(tourGroupIdList);
    }
}
